﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTier;
using SAP.Middleware.Connector;

namespace BusinessTier
{

    public class ExpenseCtr
    {
        private static ExpenseCtr _instance = null;
        private SapConnectionCtr _sapConnectionCtr = SapConnectionCtr.Instance;
        private ExpenseContainer _expCnt = ExpenseContainer.Instance;


        private ExpenseCtr()
        {
        }

        public static ExpenseCtr Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ExpenseCtr();
                }
                return _instance;
            }
        }

        /// <summary>
        /// Sends new or edited expenses to SAP, so changes can be saved. 
        /// </summary>
        public void InsertEditedExpenses()
        {
            IRfcTable table = _sapConnectionCtr.GetTableStructure();

            BuildIRfcTable(table);
            _sapConnectionCtr.InsertEditedExpenses(table);

        }
        /// <summary>
        /// Gets the expenses from SAP with searchcriterias
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="reviewed"></param>
        /// <param name="prePaid"></param>
        /// <param name="paidString"></param>
        /// <param name="toAmount"></param>
        /// <param name="fromAmount"></param>
        /// <param name="empNr"></param>
        /// <param name="types"></param>
        public void GetAllExpenses(DateTime startDate, DateTime endDate, string reviewed, string prePaid, string paidString, string toAmount, string fromAmount, string empNr, List<string> types)
        {

            IRfcTable table = _sapConnectionCtr.GetAllExpenses(startDate, endDate, reviewed, prePaid, paidString, toAmount, fromAmount, empNr, types);

            //Emptying lists before adding the new expenses
            _expCnt._expenses.Clear();
            if (table.RowCount > 0)
            {
                BuildObjects(table);
            }

         
        }
        /// <summary>
        /// Builds objects of expenses from a RfcTable.
        /// </summary>
        /// <param name="table"></param>
        private void BuildObjects(IRfcTable table)
        {
            #region buildLinks

            int pictureindex = 0;
            List<string> urlList = new List<string>();

            urlList.Add("http://www.seedsindia.org/pdf_file/1008904462sample.pdf");
            urlList.Add("http://images.oprah.com/download/pdfs/omag/omag_200709_businessplan_consulting.pdf");
            urlList.Add("http://www.screenaustralia.gov.au/getmedia/cc1a3d30-9dc5-4b0c-a6dd-42a2cf16d340/Sample_CV.pdf");
            #endregion

            for (int i = 0; i <= table.RowCount - 1; i++)
            {
                Expense expense = new Expense();

                expense.id = table[i].GetString("ID");
                expense.pspnr_ext = table[i].GetString("pspnr_ext");
                expense.posID = table[i].GetString("POSID");
                expense.wbs = table[i].GetString("POST1");
                expense.convCurrency = table[i].GetString("CONV_CURRENCY");
                expense.convAmount = table[i].GetString("CONV_AMNT");
                expense.projectDefinition = table[i].GetString("PROJECT_PSPID");
                expense.shortDescription = table[i].GetString("PROJECT_POST1");
                expense.firstName = table[i].GetString("EMPL_FIRST_NAME");
                expense.middleName = table[i].GetString("EMPL_MIDDLE_NAME");
                expense.lastName = table[i].GetString("EMPL_LAST_NAME");
                expense.expenseDescription = table[i].GetString("EXP_DESCR");
                expense.isLocked = table[i].GetString("IS_LOCK");
                expense.createDate = table[i].GetString("CREATE_DATE");
                expense.days = table[i].GetString("DAYS_NUM");
                expense.hours = table[i].GetString("HOURS_NUM");
                expense.deleteIndicator = table[i].GetString("DELETE_INDICATOR");
                expense.isChanged = table[i].GetString("IS_CHANGED");
                expense.isBilled = table[i].GetString("IS_BILLING");
                expense.approved = table[i].GetString("APPR");
                expense.reject = table[i].GetString("REJECT");
                expense.reviewed = table[i].GetString("REVIEWED");
                expense.paid = table[i].GetString("PAID");
                expense.prepaid = table[i].GetString("PREV_PAID");
                expense.expenseIndicator = table[i].GetString("EXP_IND");
                expense.transfered = table[i].GetString("TRANSFERED");
                expense.paidDate = table[i].GetString("PAID_DATE");
                expense.transferDate = table[i].GetString("TRANSF_DATE");
                expense.receiptNr = table[i].GetString("RECEIPT_NUM");
                expense.expenseDate = table[i].GetString("DEXPS");
                expense.empID = table[i].GetInt("EMPID");
                expense.pspNR = table[i].GetInt("PSPNR");
                expense.pspHI = table[i].GetInt("PSPHI");
                expense.sRule = table[i].GetString("SRULE");
                expense.amount = table[i].GetString("AMNT");
                expense.descr = table[i].GetString("DESCR");
                expense.fromDescription = table[i].GetString("FROM_DESC");
                expense.toDescription = table[i].GetString("TO_DESC");
                expense.expenseType = table[i].GetString("EXPENSE_TYPE");
                expense.distance = table[i].GetInt("DISTANCE");
                expense.origCurrency = table[i].GetString("ORIG_CURRENCY");
                expense.changeDate = table[i].GetString("CHANGE_DATE");
                expense.changeBy = table[i].GetInt("CHANGE_BY");
                expense.expSubtype = table[i].GetInt("EXP_SUBTYPE");
                expense.country = table[i].GetString("COUNTRY");
                expense.city = table[i].GetString("CITY");
                expense.hotel = table[i].GetString("HOTEL");
                expense.fromDate = table[i].GetString("FROM_DATE");
                expense.toDate = table[i].GetString("TO_DATE");
                expense.startDate = table[i].GetString("START_DATE");
                expense.endDate = table[i].GetString("END_DATE");
                expense.startTime = table[i].GetString("START_TIME");
                expense.endTime = table[i].GetString("END_TIME");
                expense.description = table[i].GetString("DESCRIPTION");
                expense.typeNumber = table[i].GetInt("TYPE_NUMBER");
                expense.subtypeNumber = table[i].GetInt("SUBTYPE_NUMBER");


                expense.pictureURL = urlList[pictureindex];


                _expCnt._expenses.Add(expense);


                if (pictureindex == 2)
                {
                    pictureindex = 0;
                }

                else
                {
                    pictureindex++;
                }

            }
        }

        /// <summary>
        /// Builds a RfcTable, and fills it with the data of the edited/new expenses
        /// </summary>
        /// <param name="table"></param>
        private void BuildIRfcTable(IRfcTable table)
        {

            foreach (Expense e in _expCnt._editedExpenses)
            {

                IRfcStructure structure = table.Metadata.LineType.CreateStructure();

                structure.SetValue("ID", e.id);
                structure.SetValue("PSPNR_EXT", e.pspnr_ext);
                structure.SetValue("POSID", e.posID);
                structure.SetValue("POST1", e.wbs);
                structure.SetValue("CONV_CURRENCY", e.convCurrency);
                structure.SetValue("CONV_AMNT", e.convAmount);
                structure.SetValue("PROJECT_PSPID", e.projectDefinition);
                structure.SetValue("PROJECT_POST1", e.shortDescription);
                structure.SetValue("EMPL_FIRST_NAME", e.firstName);
                structure.SetValue("EMPL_MIDDLE_NAME", e.middleName);
                structure.SetValue("EMPL_LAST_NAME", e.lastName);
                structure.SetValue("EXP_DESCR", e.expenseDescription);
                structure.SetValue("IS_LOCK", e.isLocked);
                structure.SetValue("CREATE_DATE", e.createDate);
                structure.SetValue("DAYS_NUM", e.days);
                structure.SetValue("HOURS_NUM", e.hours);
                structure.SetValue("DELETE_INDICATOR", e.deleteIndicator);
                structure.SetValue("IS_CHANGED", e.isChanged);
                structure.SetValue("IS_BILLING", e.isBilled);
                structure.SetValue("APPR", e.approved);
                structure.SetValue("REJECT", e.reject);
                structure.SetValue("REVIEWED", e.reviewed);
                structure.SetValue("PAID", e.paid);
                structure.SetValue("PREV_PAID", e.prepaid);
                structure.SetValue("EXP_IND", e.expenseIndicator);
                structure.SetValue("TRANSFERED", e.transfered);
                structure.SetValue("PAID_DATE", e.paidDate);
                structure.SetValue("TRANSF_DATE", e.transferDate);
                structure.SetValue("RECEIPT_NUM", e.receiptNr);
                structure.SetValue("DEXPS", e.expenseDate);
                structure.SetValue("EMPID", e.empID);
                structure.SetValue("PSPNR", e.pspNR);
                structure.SetValue("PSPHI", e.pspHI);
                structure.SetValue("SRULE", e.sRule);
                structure.SetValue("AMNT", e.amount);
                structure.SetValue("DESCR", e.descr);
                structure.SetValue("FROM_DESC", e.fromDescription);
                structure.SetValue("TO_DESC", e.toDescription);
                structure.SetValue("EXPENSE_TYPE", e.expenseType);
                structure.SetValue("DISTANCE", e.distance);
                structure.SetValue("ORIG_CURRENCY", e.origCurrency);
                structure.SetValue("CHANGE_DATE", e.changeDate);
                structure.SetValue("CHANGE_BY", e.changeBy);
                structure.SetValue("EXP_SUBTYPE", e.expSubtype);
                structure.SetValue("COUNTRY", e.country);
                structure.SetValue("CITY", e.city);
                structure.SetValue("HOTEL", e.hotel);
                structure.SetValue("FROM_DATE", e.fromDate);
                structure.SetValue("TO_DATE", e.toDate);
                structure.SetValue("START_DATE", e.startDate);
                structure.SetValue("END_DATE", e.endDate);
                structure.SetValue("START_TIME", e.startTime);
                structure.SetValue("END_TIME", e.endTime);
                structure.SetValue("DESCRIPTION", e.description);
                structure.SetValue("TYPE_NUMBER", e.typeNumber);
                structure.SetValue("SUBTYPE_NUMBER", e.subtypeNumber);

                table.Insert(structure);
            }

        }

        /// <summary>
        /// Copies an expense, so there are two objects with exactly the same attributes, except for id, reviewed and receiptNr,
        /// which is set so it can be added in SAP as a new expense
        /// </summary>
        /// <param name="expense"></param>
        /// <returns></returns>
        public Expense CopyExpense(Expense expense)
        {
            Expense clonedExpense = new Expense();

            clonedExpense.id = "";
            clonedExpense.pspnr_ext = expense.pspnr_ext;
            clonedExpense.posID = expense.posID;
            clonedExpense.wbs = expense.wbs;
            clonedExpense.convCurrency = expense.convCurrency;
            clonedExpense.convAmount = expense.convAmount;
            clonedExpense.projectDefinition = expense.projectDefinition;
            clonedExpense.shortDescription = expense.shortDescription;
            clonedExpense.firstName = expense.firstName;
            clonedExpense.middleName = expense.middleName;
            clonedExpense.lastName = expense.lastName;
            clonedExpense.expenseDescription = expense.expenseDescription;
            clonedExpense.isLocked = expense.isLocked;
            clonedExpense.createDate = expense.createDate;
            clonedExpense.days = expense.days;
            clonedExpense.hours = expense.hours;
            clonedExpense.deleteIndicator = expense.deleteIndicator;
            clonedExpense.isChanged = expense.isChanged;
            clonedExpense.isBilled = expense.isBilled;
            clonedExpense.approved = expense.approved;
            clonedExpense.reject = expense.reject;
            clonedExpense.reviewed = "";
            clonedExpense.paid = expense.paid;
            clonedExpense.prepaid = expense.prepaid;
            clonedExpense.expenseIndicator = expense.expenseIndicator;
            clonedExpense.transfered = expense.transfered;
            clonedExpense.paidDate = expense.paidDate;
            clonedExpense.transferDate = expense.transferDate;
            clonedExpense.receiptNr = "0000";
            clonedExpense.expenseDate = expense.expenseDate;
            clonedExpense.empID = expense.empID;
            clonedExpense.pspNR = expense.pspNR;
            clonedExpense.pspHI = expense.pspHI;
            clonedExpense.sRule = expense.sRule;
            clonedExpense.amount = expense.amount;
            clonedExpense.descr = expense.descr;
            clonedExpense.fromDescription = expense.fromDescription;
            clonedExpense.toDescription = expense.toDescription;
            clonedExpense.expenseType = expense.expenseType;
            clonedExpense.distance = expense.distance;
            clonedExpense.origCurrency = expense.origCurrency;
            clonedExpense.changeDate = expense.changeDate;
            clonedExpense.changeBy = expense.changeBy;
            clonedExpense.expSubtype = expense.expSubtype;
            clonedExpense.country = expense.country;
            clonedExpense.city = expense.city;
            clonedExpense.hotel = expense.hotel;
            clonedExpense.fromDate = expense.fromDate;
            clonedExpense.toDate = expense.toDate;
            clonedExpense.startDate = expense.startDate;
            clonedExpense.endDate = expense.endDate;
            clonedExpense.startTime = expense.startTime;
            clonedExpense.endTime = expense.endTime;
            clonedExpense.description = expense.description;
            clonedExpense.typeNumber = expense.typeNumber;
            clonedExpense.subtypeNumber = expense.subtypeNumber;

            return clonedExpense;
        }
    }
}
